package com.pqtest.api.service;

import com.pqtest.api.domain.ConnectionDomain;
import com.pqtest.api.dto.CollectionDto;
import com.pqtest.api.dto.ConnectionDto;
import com.pqtest.api.dto.SimulibraryDto;
import com.pqtest.api.entity.ConnectionEntity;
import com.pqtest.api.entity.SimulibraryEntity;
import com.pqtest.api.mapper.CollectionMapper;
import com.pqtest.api.mapper.ConnectionMapper;
import com.pqtest.api.utils.DtoEntityTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class ConnectionService {
    @Autowired
    ConnectionMapper connectionMapper;
    @Autowired
    ConnectionDomain connectionDomain;
    @Autowired
    CollectionService collectionService;

    public int collect(ConnectionDto connectionDto,int uid) throws Exception {
        ConnectionEntity connectionEntity = connectionDomain.collected(connectionDto);
        //ConnectionDto connectionDto1 =  this.getOne(connectionDto.getItemId());
        CollectionDto collectionDto = new CollectionDto();
        collectionDto.setUserId(uid);
        collectionDto.setItemId(connectionDto.getItemId());
        collectionService.collect(collectionDto);
        return connectionMapper.collected(connectionEntity);
    }

    public int deCollect(ConnectionDto connectionDto,int uid){
        ConnectionEntity connectionEntity = connectionDomain.collected(connectionDto);
        //ConnectionDto connectionDto1 =  this.getOne(connectionDto.getItemId());
        CollectionDto collectionDto = new CollectionDto();
        collectionDto.setUserId(uid);
        collectionDto.setItemId(connectionDto.getItemId());
        collectionService.deCollect(collectionDto);
        return connectionMapper.collected(connectionEntity);
    }
    public ConnectionDto getOne(int id ){
        ConnectionEntity connectionEntity = connectionMapper.getOne(id);
        ConnectionDto connectionDto = new ConnectionDto();
        DtoEntityTransformer.transalte(connectionEntity,connectionDto);
        return connectionDto;
    }

    public List<ConnectionEntity> getByLibrary(SimulibraryDto simulibraryDto){
        SimulibraryEntity simulibraryEntity = new SimulibraryEntity();
        DtoEntityTransformer.transalte(simulibraryDto,simulibraryEntity);
        return connectionMapper.getByLibrary(simulibraryEntity);
    }

    public List<ConnectionEntity> getDeepByLibrary(SimulibraryDto simulibraryDto){
        SimulibraryEntity simulibraryEntity = new SimulibraryEntity();
        DtoEntityTransformer.transalte(simulibraryDto,simulibraryEntity);
        return connectionMapper.getDeepByLibrary(simulibraryEntity);
    }
    public void getReport(){

    }
}
